package ynu.ehall.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import ynu.ehall.dao.IUserDao;
import ynu.ehall.po.User;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping(value = "/user")
@Tag(name="用户信息相关服务")
public class UserController {
    @Resource
    private IUserDao userDao;

    @Operation(summary = "登录服务")
    @Parameters({
            @Parameter(name="id" ,description = "用户名"),
            @Parameter(name="pwd" ,description = "密码")
    })
    @PostMapping(value = "/login")
    public User login(@RequestParam(value = "id") String userId, @RequestParam(value = "pwd") String password) {
        return userDao.findUserByUserIdAndPassword(Long.valueOf(userId), password);
    }

    @Operation(summary = "用户注册")
    @Parameter(name="user",description = "用户信息JSON格式")
    @PostMapping(value = "/createUser")
    public User insert(@RequestBody User user) {
        return userDao.save(user);
    }

    @Operation(summary = "修改用户信息")
    @Parameter(name="user",description = "用户信息JSON格式")
    @PutMapping(value = "/updateUser")
    public Boolean update(@RequestBody User user) {
        userDao.save(user);
        return true;
    }

    @Operation(summary = "删除用户")
    @Parameter(name="id",description = "用户名")
    @DeleteMapping(value = "/deleteUser")
    public Boolean delete(@RequestParam(value = "id")String userId) {
        userDao.deleteUserByUserId(Long.valueOf(userId));
        return true;
    }

    @Operation(summary = "查找女性用户")
    @GetMapping(value = "/findUser")
    public List<User> findUser() {
        return userDao.findUsersBySexFalse();
    }

    @Operation(summary = "查找所有用户信息")
    @GetMapping(value = "/findAll")
    public List<User> findAllUsers() {
        return StreamSupport.stream(userDao.findAll().spliterator(),false)
                .collect(Collectors.toList());
    }
}
